// const fs = require('fs')
let webpack = require('webpack')
// const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const CompressionWebpackPlugin = require('compression-webpack-plugin')

// let path = require('path')
module.exports = {
  // 修改的配置
  // 将baseUrl: '/api',改为baseUrl: '/',
  // baseUrl: "/",
  publicPath: process.env.NODE_ENV === 'production' ? '/resources/mobile/mall/xn/newm/' : '/',
  devServer: {
    // port: 8083,
    disableHostCheck: true,
    proxy: {
      '/api': {
        target: 'http://lightgen.genebook.com.cn',
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      },
      '/malltest': {
        target: 'http://malltest.genebook.com.cn',
        changeOrigin: true,
        pathRewrite: {
          '^/malltest': ''
        }
      },
      '/mall': {
        target: 'http://mall.genebook.com.cn',
        changeOrigin: true,
        pathRewrite: {
          '^/mall': ''
        }
      },
      '/logintest': {
        target: 'http://logintest.genebook.com.cn',
        changeOrigin: true,
        pathRewrite: {
          '^/logintest': ''
        }
      },
      '/login': {
        target: 'https://login.genebook.com.cn',
        changeOrigin: true, //是否跨域
        secure: false, //如果是https接口，需要配置这个参数
        pathRewrite: {
          '^/login': ''
        }
      }
    }
  },
  pwa: {
    name: 'genebook',
    themeColor: '#39b6ff',
    msTileColor: '#000000',
    appleMobileWebAppCapable: 'yes',
    appleMobileWebAppStatusBarStyle: '#39b6ff'
  },
  productionSourceMap: false,
  css: {
    // 是否使用css分离插件 ExtractTextPlugin
    extract: process.env.NODE_ENV === 'production' ? true : false,
    // 开启 CSS source maps?
    sourceMap: true,
    // css预设器配置项
    loaderOptions: {
      sass: {
        data: `@import "@/assets/css/common.scss";` //向所有 Sass 样式传入共享的全局变量
        // data: fs.readFileSync('src/assets/css/common.scss', 'utf-8')
      },
      postcss: {
        plugins: [
          require('postcss-pxtorem')({
            rootValue: 37.5, // 换算的基数
            selectorBlackList: ['.swiper-', '.layui-', '.norem'], // 忽略转换正则匹配项
            propList: ['*'],
            minPixelValue: 3 // 替换的最小像素值
          })
        ]
      }
    },
    // 启用 CSS modules for all css / pre-processor files.
    modules: false
  },
  configureWebpack: {
    plugins: [
      new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery',
        'windows.jQuery': 'jquery'
      }),
      new CompressionWebpackPlugin({
        asset: '[path].gz[query]',
        algorithm: 'gzip',
        test: /\.js$|\.css$/,
        threshold: 10240, //对超过10kb的文件进行压缩
        // deleteOriginalAssets: true, //不删除源文件，不建议删除源文件
        minRatio: 0.8
      })
    ],
    optimization: {
      // minimizer: [
      //   new UglifyJsPlugin({
      //     uglifyOptions: {
      //       compress: {
      //         warnings: false,
      //         drop_console: true, //console
      //         drop_debugger: false,
      //         pure_funcs: ['console.log'] //移除console
      //       }
      //     }
      //   })
      // ]
    },
    resolve: {
      alias: {
        vue$: 'vue/dist/vue.esm.js' //从Compiler版本修改为Runtime版本
      }
    }
  }
}
